package singleton;

import java.io.Serializable;

/**
 * 懒汉式，要注意线程安全问题破环单例
 */
public class Singleton3 implements Serializable {
    private Singleton3(){
        System.out.println("private Singleton3()");
    }

    private static Singleton3 INSTANCE = null;

    //加入synchronized防止多线程安全问题,虽能解决，但严重影响性能
    public static synchronized Singleton3 getInstance(){
        if(INSTANCE == null){
            INSTANCE = new Singleton3();
        }
        return INSTANCE;
    }

    //防止反序列化破环单例
    public Object readResolve(){
        return INSTANCE;
    }
}
